package com.zhang.common.http;

import javax.servlet.http.Cookie;
import java.io.IOException;
import java.util.Map;

/**
 * 使用方式(例)：
 * String responseBody = CharacterHttpRequester.post(url,data,"application/json");
 * List<HashMap> list = JSON.parseObject(responseBody, List.class);
 * @author zhangxin
 * @date 2021/6/23
 */
public class CharacterHttpRequester {
    private static HttpClient hc = new HttpClient();

    public CharacterHttpRequester() {
    }

    public static String sendRequest(String url, String method, String data) throws IOException {
        return sendRequest(url, method, data, (Cookie[])null);
    }

    public static String post(String url, String data, String contentType) throws IOException {
        return post(url, data, contentType, (Cookie[])null);
    }

    public static String sendRequest(String url, String method, String data, Cookie[] cookies) throws IOException {
        return sendRequest(url, method, data, cookies, (Map)null);
    }

    public static String post(String url, String data, String contentType, Cookie[] cookies) throws IOException {
        return post(url, data, contentType, cookies, (Map)null);
    }

    public static String sendRequest(String url, String method, String data, Cookie[] cookies, Map<String, String> session) throws IOException {
        if(url != null && method != null) {
            url = url.trim();
            method = method.trim().toUpperCase();
            if(!url.startsWith("http://") && !url.startsWith("https://")) {
                return null;
            } else {
                Cookie[] result;
                if(cookies != null && cookies.length > 0) {
                    result = cookies;
                    int var6 = cookies.length;

                    for(int var7 = 0; var7 < var6; ++var7) {
                        Cookie c = result[var7];
                        hc.addCookie(c.getName(), c.getValue());
                    }
                }

                result = null;
                byte[] result1;
                if(method.equals("POST")) {
                    result1 = hc.post(url, data);
                } else {
                    if(data != null && data.length() > 0) {
                        if(url.indexOf("?") >= 0) {
                            url = url + "&" + data;
                        } else {
                            url = url + "?" + data;
                        }
                    }

                    result1 = hc.get(url);
                }

                if(session != null) {
                    session.putAll(hc.getSession());
                }

                String s = new String(result1, "UTF-8");
                return s;
            }
        } else {
            return null;
        }
    }

    public static String post(String url, String data, String contentType, Cookie[] cookies, Map<String, String> session) throws IOException {
        if(!url.startsWith("http://") && !url.startsWith("https://")) {
            return null;
        } else {
            if(cookies != null && cookies.length > 0) {
                Cookie[] var5 = cookies;
                int var6 = cookies.length;

                for(int var7 = 0; var7 < var6; ++var7) {
                    Cookie c = var5[var7];
                    hc.addCookie(c.getName(), c.getValue());
                }
            }

            byte[] result = hc.post(url, data, contentType);
            if(session != null) {
                session.putAll(hc.getSession());
            }

            String s = new String(result, "UTF-8");
            return s;
        }
    }

    static {
        hc.setEncoding("UTF-8");
    }
}